﻿using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using MsmqView.Core.Configuration;
using MsmqView.Core.Messaging;
using MsmqView.UI.Controllers;
using MsmqView.UI.EventArgs;
using MsmqView.UI.Views;
using MsmqView.Core.Security;

namespace MsmqView.UI
{
	/// <summary>
	/// Interaction logic for Window1.xaml
	/// </summary>
	public partial class MsmqViewMain : Window, IMsmqViewMainView
	{
		private readonly IMsmqViewMainController controller;

		public MsmqViewMain()
		{
			InitializeComponent();

			//ISecurityGateway securityGateway = new SecurityGateway();
			//securityGateway.ImpersonateUser("engdev", "rgray", "Rh!n0Rh0cks");

			var environment = new EnvironmentConfiguration();
			var gateway = new MessageQueueGateway();
			controller = new MsmqViewMainController(this, environment, gateway);

			showQueuesButton.Click += new RoutedEventHandler(showQueuesButton_Click);
			queuesList.SelectionChanged += new SelectionChangedEventHandler(queuesList_SelectionChanged);
			messageList.SelectionChanged += new SelectionChangedEventHandler(messageList_SelectionChanged);
		}

		private void showQueuesButton_Click(object sender, RoutedEventArgs e)
		{
			OnMachineSelected(machineNameTextBox.Text);
		}

		private void messageList_SelectionChanged(object sender, SelectionChangedEventArgs e)
		{
			var selectedItem = ((ListBox)sender).SelectedItem as MessageIdentifier;
			if (selectedItem != null)
			{
				OnMessageSelected(selectedItem);
			}
		}

		private void queuesList_SelectionChanged(object sender, SelectionChangedEventArgs e)
		{
			var selectedQueue = ((ListBox)sender).SelectedItem as QueueName;
			if (selectedQueue != null)
			{
				OnQueueSelected(selectedQueue);
			}
		}

		private void OnMachineSelected(string machineName)
		{
			if (MachineSelected != null)
			{
				MachineSelected(this, new MachineSelectedEventArgs { SelectedMachineName = machineName });
			}
		}

		private void OnQueueSelected(QueueName selectedQueue)
		{
			if (QueueSelected != null)
			{
				QueueSelected(this, new QueueSelectedEventArgs { SelectedQueue = selectedQueue }); 
			}
		}

		private void OnMessageSelected(MessageIdentifier selectedMessageIdentifier)
		{
			if (MessageSelected != null)
			{
				MessageSelected(this, new MessageSelectedEventArgs { SelectedMessageIdentifier = selectedMessageIdentifier });
			}
		}

		#region IMsmqViewMainView Members

		public event EventHandler<MachineSelectedEventArgs> MachineSelected;

		public event EventHandler<QueueSelectedEventArgs> QueueSelected;

		public event EventHandler<MessageSelectedEventArgs> MessageSelected;

		public void DisplayError(string errorMessage)
		{
			errorLabel.Content = errorMessage;
		}

		public void DisplayMachineName(string machineName)
		{
			machineNameTextBox.Text = machineName;
		}

		public void DisplayQueues(IEnumerable<QueueName> queueNames)
		{
			queuesList.ItemsSource = queueNames;
			queuesList.DisplayMemberPath = "FriendlyName";
		}

		public void DisplayMessages(IEnumerable<MessageIdentifier> messageIdentifiers)
		{
			messageList.ItemsSource = messageIdentifiers;
			messageList.DisplayMemberPath = "FriendlyIdentifier";
		}

		public void DisplayMessageBody(string messageBody)
		{
			messageBodyBox.Text = messageBody;
		}

		#endregion
	}
}
